﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using MolyMade.Http;
using MolyMade.Http.DataTypes;
using MolyMade.Http.Enums;
using MolyMade.Http.Statics;
using MolyMade.Http.Utilities;

namespace zhihuSpider
{
    class First
    {
        private string _frontPage = @"https://www.zhihu.com/#signin";
        private readonly Headers _frontHeaders = new Headers()
        {
            Accept = @"	text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            AcceptEncoding = @"	gzip, deflate, br",
            AcceptLanguage = @"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
            CacheControl = "no - cache",
            KeepAlive = true,
            Host = "www.zhihu.com",
            Pragma = "no-cache",
            UserAgent = UserAgents.Ie9
        };

        public Response Get()
        {
            return Requests.Get(_frontPage, headers: _frontHeaders);
        }

        public string GetXlrf()
        {
            string reg = "input type=\"hidden\" name=\"_xsrf\" value=\"(\\S+)\"/>";
            Regex regex = new Regex(reg);
            Match match = regex.Match(Get().Text);
            return match.Groups[1].Value;
        }
    }

    class Second 
    {
        private string _loginUrl = @"http://www.zhihu.com/login/email";
        private Headers _loginHeaders = new Headers()
        {
            Cookie = " cap_id=\"YjA5MjE0YzYyNGQ2NDY5NWJhMmFhN2YyY2EwODIwZjQ=|1437610072|e7cc307c0d2fe2ee84fd3ceb7f83d298156e37e0\"; ",
            Accept = "*/*",
            AcceptEncoding = "gzip,deflate",
            AcceptLanguage = "zh-CN,zh;q=0.8",
            CacheControl = "no-cache",
            KeepAlive = true,
            ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
            Host = "www.zhihu.com",
            Pragma = "no-cache",
            Referer = @"http://www.zhihu.com/",
            UserAgent = UserAgents.Ie9,
            CustomPairs = new Dictionary<string, string>()
            {
                ["Origin"] = "http://www.zhihu.com",
                ["X-Requested-With"] = "XMLHttpRequest"
            }
        };

        public string Post(string email, string password, string xsrf)
        {
            string postString = Utilities.ConcatParameters(new Dictionary<string, string>()
            {
                ["_xsrf"] = xsrf,
                ["password"] = password,
                ["remeber_me"] = "true",
                ["email"] = email
            });
            Response r = Requests.Post(_loginUrl, headers: _loginHeaders, postString: postString);
            return r.Text;

        }
    }
}
